package de.bbl.android.tools;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.net.http.AndroidHttpClient;
import de.bbl.android.BBLAndroidFragmentActivity;
import de.bbl.android.R;

public class Tools {
	
	public static boolean getGamedayData(String mainUrl, String additionalUrl, String contextFile, String outputPath) {
		
		boolean retVal = false;
		String requestString = mainUrl + additionalUrl + ".md5";
		
    	HttpClient client = new DefaultHttpClient();
    	HttpGet request = new HttpGet(requestString);
    	HttpResponse response = null;
    	BufferedReader rd = null;
		String md5Checksum = "";

    	try {
			response = client.execute(request);
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			md5Checksum = rd.readLine();
	    	rd.close();
	    	
	    	File file = new File(outputPath + contextFile + ".md5");
	    	if (file.exists()) {
	    		FileInputStream fstream = new FileInputStream(file);
	    		DataInputStream in = new DataInputStream(fstream);
	    		BufferedReader br = new BufferedReader(new InputStreamReader(in));
	    		String strLine;
	    		while ((strLine = br.readLine()) != null)   {
	    		    break;
	    		}	
	    		br.close();
	    		File fileOutput = new File(outputPath + contextFile);
	    		if (strLine.equals(md5Checksum) && fileOutput.exists()) {
	    			return true;
	    		} else {
		    		FileWriter write = new FileWriter(file);
		    		write.write(md5Checksum);
		    		write.close();	    		
	    		}	    		
	    	} else {
	    		FileWriter write = new FileWriter(file);
	    		write.write(md5Checksum);
	    		write.close();	    		
	    	}
	    	
    	} catch (ClientProtocolException e) {
    		AppendToErrorFile(e.getMessage());
			// e.printStackTrace();
		} catch (IllegalStateException e) {
    		AppendToErrorFile(e.getMessage());
			// e.printStackTrace();
		} catch (IOException e) {
    		AppendToErrorFile(e.getMessage());
			// e.printStackTrace();
		}

    	requestString = mainUrl + additionalUrl + ".gz";
    	request = new HttpGet(requestString);
    	
    	request.addHeader("Accept-Encoding", "gz");
    	response = null;
    	rd = null;
    	try {
    		AndroidHttpClient.modifyRequestToAcceptGzipResponse( request );
    		response = client.execute(request);
			
    		InputStream inputStream = AndroidHttpClient.getUngzippedContent( response.getEntity() );
			
    		GZIPInputStream zis = new GZIPInputStream(inputStream);
			File file = new File(outputPath + contextFile);
			OutputStream out = new FileOutputStream(file);
			
			int len = 0; 
			byte buf[] = new byte[1024];
			
			while( (len=zis.read(buf)) > 0)
				out.write(buf,0,len);
			
			out.close();
			inputStream.close();
			zis.close();
    	} catch (ClientProtocolException e) {
    		AppendToErrorFile(e.getMessage());
			// e.printStackTrace();
		} catch (IllegalStateException e) {
    		AppendToErrorFile(e.getMessage());
			// e.printStackTrace();
		} catch (IOException e) {
    		AppendToErrorFile(e.getMessage());
			// e.printStackTrace();
		} finally {
			if (new File(outputPath + contextFile).exists())
				retVal = true;
			else
				retVal = false;
		}
    	
    	return retVal;
    }

	public static boolean getMagazinData(String mainUrl, String additionalUrl, String contextFile, String outputPath) {
		boolean retVal = false;
		String requestString = mainUrl + additionalUrl;

		HttpClient client = new DefaultHttpClient();
    	HttpGet request = new HttpGet(requestString);
    	HttpResponse response = null;
    	request.addHeader("Accept-Encoding", "gz");
    	response = null;
    	try {
    		response = client.execute(request);
    		InputStream inputStream = AndroidHttpClient.getUngzippedContent( response.getEntity() );
			
			File file = new File(outputPath + contextFile);
			OutputStream out = new FileOutputStream(file);
			
			int len = 0; 
			byte buf[] = new byte[1024];
			
			while( (len=inputStream.read(buf)) > 0)
				out.write(buf,0,len);
			
			out.close();
			inputStream.close();
    	} catch (ClientProtocolException e) {
    		AppendToErrorFile(e.getMessage());
			// e.printStackTrace();
		} catch (IllegalStateException e) {
    		AppendToErrorFile(e.getMessage());
			// e.printStackTrace();
		} catch (IOException e) {
    		AppendToErrorFile(e.getMessage());
			// e.printStackTrace();
		} finally {
			if (new File(outputPath + contextFile).exists())
				retVal = true;
			else
				retVal = false;
		}
    	
    	return retVal;
	}

	public static boolean isSdPresent() {
	    return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
	}

	public static void AppendToErrorFile(String message) {
		File errorFile = new File(BBLAndroidFragmentActivity.bbl_sd_path + "error.log");
		try {
			if (! errorFile.exists())
				errorFile.createNewFile();
			
			FileWriter fw = new FileWriter(errorFile);
			fw.append(message);
			fw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
			
	}

	public static int loadTeamImage(int teamId) {
		
		int retval = 0;
		
		switch(teamId){
		case 413:
			retval = R.drawable.id_413;
			break;
		case 414:
			retval = R.drawable.id_414;
			break;
		case 415:
			retval = R.drawable.id_415;
			break;
		case 418:
			retval = R.drawable.id_418;
			break;
		case 420:
			retval = R.drawable.id_420;
			break;
		case 421:
			retval = R.drawable.id_421;
			break;
		case 422:
			retval = R.drawable.id_422;
			break;
		case 425:
			retval = R.drawable.id_425;
			break;
		case 426:
			retval = R.drawable.id_426;
			break;
		case 428:
			retval = R.drawable.id_428;
			break;
		case 430:
			retval = R.drawable.id_430;
			break;
		case 432:
			retval = R.drawable.id_432;
			break;
		case 433:
			retval = R.drawable.id_433;
			break;
		case 435:
			retval = R.drawable.id_435;
			break;
		case 439:
			retval = R.drawable.id_439;
			break;
		case 473:
			retval = R.drawable.id_473;
			break;
		case 486:
			retval = R.drawable.id_486;
			break;
		case 540:
			retval = R.drawable.id_540;
			break;
		default:
			retval = android.R.drawable.ic_delete;
			break;
		}
		return retval;
	}

}
